package com.bite.f.service.exam.impl;

import com.bite.common.core.domain.TableDataInfo;
import com.bite.common.redis.service.RedisService;
import com.bite.f.domain.exam.dto.ExamQueryDTO;
import com.bite.f.domain.exam.vo.ExamVO;
import com.bite.f.manager.ExamCacheManager;
import com.bite.f.mapper.exam.ExamMapper;
import com.bite.f.service.exam.IExamService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class ExamServiceImpl implements IExamService {
    @Autowired
    private ExamMapper examMapper;
    @Autowired
    private ExamCacheManager examCacheManager;
    @Override
    public List<ExamVO> list(ExamQueryDTO examQueryDTO) {
        PageHelper.startPage(examQueryDTO.getPageNum(), examQueryDTO.getPageSize());
        return examMapper.selectExamList(examQueryDTO);
    }

    @Override
    public TableDataInfo redisList(ExamQueryDTO examQueryDTO) {
        //先从redis中查询，如果redis中没有数据，那么就从数据库中获取，并刷新缓存
        Long total = examCacheManager.getListSize(examQueryDTO.getType(),1L);
        List<ExamVO> examVOList;
        if(total == null || total == 0){
            //从数据库中查寻
            examVOList = list(examQueryDTO);
            examCacheManager.refreshCache(examQueryDTO.getType(),1L);
            total = new PageInfo<>(examVOList).getTotal();
        }{
            //从缓存中查询数据
            examVOList = examCacheManager.getExamVOList(examQueryDTO,1l);
            //为了防止因为redis中数据出现异常重新刷新redis，因此我们要在用一次重新获取的方法
            total = examCacheManager.getListSize(examQueryDTO.getType(),1L);
        }
        return  TableDataInfo.success(examVOList,total);
    }
}





























